import Vue from 'vue'
import VueI18n from 'vue-i18n'
import Cookies from 'js-cookie'
import elementEnLocale from 'element-ui/lib/locale/lang/en' // element-ui lang
import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN'// element-ui lang
import elementEsLocale from 'element-ui/lib/locale/lang/es'// element-ui lang
import { getSelect } from '@/api/system/language'
import enLocale from './en'
import zhLocale from './zh'
import esLocale from './es'
import deLocale from './de'
import frLocale from './fr'
import ptLocale from './Portugal'
import ruLocale from './ru'
import arLocale from './ar'
import vnLocale from './Vietnam'
import itLocale from './it'
import inLocale from './Indonesia'
import thLocale from './th'

Vue.use(VueI18n)

const messages = {
  '中文': {
    ...zhLocale
  },
  'English': {
    ...enLocale
  },
  'Indonesian': {
    ...inLocale
  },
  'ไทย': {
    ...thLocale
  },
  'Tiếng Việt': {
    ...vnLocale
  },
  'عربى': {
    ...arLocale
  },
  'Español': {
    ...esLocale
  },
  'Français': {
    ...frLocale
  },
  'Deutsche': {
    ...deLocale
  },
  'Português': {
    ...ptLocale
  },
  'Pусский': {
    ...ruLocale
  },
  'Italiano': {
    ...itLocale
  }
}
// console.log(JSON.stringify(elementEsLocale),'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee');
// 获取数据库中的语言
// getSelect().then(res => {
//   res.data.list.forEach(item => {
//     if (typeof (JSON.parse(JSON.parse(JSON.stringify(item.json)))) == 'object') {
//       messages[item.language] = JSON.parse(JSON.parse(JSON.stringify(item.json)))
//     }

//   })
//   console.log(messages,'MMMMMMMMMMMMMMMMMMMMMMMMMMM')
// })

async function getLanguage() {
  const data = await getSelect()
  console.log('执行了')
  data.data.list.forEach(item => {
    if (typeof (JSON.parse(JSON.parse(JSON.stringify(item.json)))) == 'object') {
      messages[item.language] = JSON.parse(JSON.parse(JSON.stringify(item.json)))
    }
  })
}
getLanguage()
const i18n = new VueI18n({
  // set locale
  // options: en | zh | es
  locale: Cookies.get('language') || 'English',
  // set locale messages
  messages
})

export default i18n
